﻿using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

namespace Web.Areas.Admin.Helpers
{
    public class ImageHandler : IHttpHandler
    {
        private Manager.BLL.Admin.M_SiteBll bll = new Manager.BLL.Admin.M_SiteBll();
        
        public ImageHandler()
        {
        }

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //获取请求的物理图片路径
            string watermarkurl = "";
            if (bll.Exists(1))
                watermarkurl = bll.GetModel(1).WaterMarkUrl;
            else
                watermarkurl = System.Configuration.ConfigurationManager.AppSettings["watermark"].ToString();

            string imagePath = context.Request.PhysicalPath;
            Bitmap image = null;
            Image waterMark = Image.FromFile(context.Request.MapPath(watermarkurl));

            if (context.Cache[imagePath] == null)//如果当前缓存中没有指定的图片就将该图片添加水印并缓存
            {
                image = new Bitmap(imagePath);
                image = AddWaterMark(image, waterMark);
                context.Cache[imagePath] = image;
            }
            else//否则就直接从混存中取出添加了水印的图片，节省时间
            {
                image = context.Cache[imagePath] as Bitmap;
            }

            context.Response.ContentType = "image/Jpeg";
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//将添加水印的图片输入到当前流中
        }

        //给图片添加水印
        private Bitmap AddWaterMark(Bitmap image, Image mark)
        {
            Manager.Model.Admin.M_Site site=new Manager.Model.Admin.M_Site();
            if(bll.Exists(1))
                site=bll.GetModel(1);
            else
                return image;
            if(!site.WaterMark)
                return image;

            int markLeft = 10;
            int markTop = 10;

            if (image.Width > 200)
            {
                if (!site.WaterMarkType)
                {

                    string text = site.WaterMarkName;
                    int fontSize = 12;
                    Font font = new Font("宋体", fontSize, FontStyle.Bold);

                    //Brush brush = Brushes.DarkGray;
                    Brush brush = Brushes.Green;
                    Graphics g = Graphics.FromImage(image);
                    SizeF size = g.MeasureString(text, font);

                    if (site.WaterMarkPosition == 3)
                    {
                        markLeft = image.Width - (int)size.Width - 10;
                        markTop = image.Height - (int)size.Height - 10;
                    }
                    else if (site.WaterMarkPosition == 2)
                    {
                        markLeft = image.Width - (int)size.Width - 10;
                        markTop = 10;
                    }
                    else if (site.WaterMarkPosition == 4)
                    {
                        markLeft = 10;
                        markTop = image.Height - (int)size.Height - 10;
                    }

                    g.DrawString(text, font, brush, markLeft, markTop);
                    g.Dispose();
                    return image;
                }
                else
                {
                    if (site.WaterMarkPosition == 3)
                    {
                        markLeft = image.Width - mark.Width - 10;
                        markTop = image.Height - mark.Height - 10;
                    }
                    else if (site.WaterMarkPosition == 2)
                    {
                        markLeft = image.Width - mark.Width - 10;
                        markTop = 10;
                    }
                    else if (site.WaterMarkPosition == 4)
                    {
                        markLeft = 10;
                        markTop = image.Height - mark.Height - 10;
                    }
                    //设置颜色矩阵 
                    int lucencyPercent = site.WaterMarkAlpha.Value;
                    float[][] matrixItems ={ 
                    new float[] {1, 0, 0, 0, 0}, 
                    new float[] {0, 1, 0, 0, 0}, 
                    new float[] {0, 0, 1, 0, 0}, 
                    new float[] {0, 0, 0, (float)lucencyPercent/100f, 0}, 
                    new float[] {0, 0, 0, 0, 1}};

                    ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
                    ImageAttributes imgAttr = new ImageAttributes();
                    imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                    Graphics g = Graphics.FromImage(image);
                    g.DrawImage(mark, new Rectangle(markLeft, markTop, mark.Width, mark.Height), 0, 0, mark.Width, mark.Height, GraphicsUnit.Pixel, imgAttr);
                    g.Dispose();
                    mark.Dispose();
                    return image;
                }
            }
            else
                return image;
        }
    }
}